/**
*	插件：公共列表页
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.list = {
	init:function(){
		
	},
	create:function(cans){
		var me = this;
		var defv = js.applyIf(cans,{
			rand:js.getrand(),
			num:'',  //模块编号
			cnum:'',
			funitid:'',
			pnum:'',
			atype:'',
			gtype:'',
			name:'', 
			view:'',
			showtype:'',
			moders:{},
			kzobj:{}, //扩展
			setcinfo:{},
			optobj:{},
			params:{},//参数
			iscompany:false,
			ismodal:false,
			store:{},
			modejs:'', 
			tablecans:{}, //表格参数
			ispl:true,
			fieldsarr:[],
			xiangwidth:700,
			onitemclick:function(){},
			onitemdblclick:function(){},
			onaddbefore:function(){},
			onloadbefore:function(){},
			onmenushow:function(){},
			onloadtable:function(){},
			onsearchbefore:function(){},
			onmenusavebefore:function(){}
		});
		for(var i in defv)this[i]=defv[i];
		var rand  = this.rand;
		
		this.init = function(){
			if(this.params.otype=='company')this.iscompany = true;
			if(this.modejs){
				var url = 'res/flow/'+this.num+'.js';
				js.importjs(url, function(){
					me.modejsshow();
				});
			}else{
				me.modejsshow();
			}
		};
		
		this.modejsshow = function(){
			var key  = ''+this.num+'_list';
			if(js[key]){
				var nobj = new js[key]();
				for(var i in nobj)this[i] = nobj[i];
				nobj.listobj = this;
				this.kzobj	 = nobj;
				if(this.initbodys)this.initbodys();
			}
			importjs('table', function(){
				me.createtable();
			});
		};
		
		this.resize = function(){
			if(this.table)this.table.resize();
		};
		
		this.createtable = function(){
			var cans 	= js.apply({
				view:this.view,
				celleditor:true,
				url:'api/flow/data?num='+this.num+'',
				urledit:'api/flow/editcell?num='+this.num+'',
				urldelete:'api/flow/delete',
				autoheight:true,
				num:this.num,
				cnum:this.cnum,
				funitid:this.funitid,
				xiangwidth:this.xiangwidth,
				fanye:true,
				showtype:this.showtype,
				pageSize: (this.showtype=='1') ? 10 :15,
				caozuo:true,
				urlparams:{},
				listobj:this,
				columns:[{
					text:'加载中...',dataIndex:'text1'
				}],
				onloadbefore:function(d){
					me.loadbefore(d);
				},
				onitemclick:function(d){
					me.onitemclick(d);
				},
				onitemdblclick:function(d){
					me.onitemdblclick(d);
					me.xiang(d.id, d);
				},
				onload:function(d){
					me.onloadtable(d);
				}
			}, this.tablecans);
			if(this.pnum)cans.urlparams.pnum   = this.pnum;
			if(this.atype)cans.urlparams.atype = this.atype;
			if(this.gtype)cans.urlparams.gtype = this.gtype;
			if(this.sqlwhere)cans.urlparams.sqlwhere = jm.base64encode(this.sqlwhere);
			this.table = new js.table.create(cans);
			this.kzobj.table 	= this.table;
		};
		
		this.settable = function(cans){
			for(var i in cans)this.tablecans[i]=cans[i];
		};
		
		
		this.loadbefore=function(d){
			if(d.fieldsarr)this.initfields(d.fieldsarr);
			if(d.moders){
				this.name   	= d.moders.name;
				this.isflow 	= d.moders.isflow;
				this.table.name = this.name;
			}
			this.onloadbefore(d);
		},
		this.initfields=function(farr){
			this.fieldsarr = farr;
			var da = [],i,d,d1,d2;
			for(i=0;i<farr.length;i++){
				d = farr[i];
				if(d.islb=='1'){
					d1 = {dataIndex:d.fields,text:d.name,type:d.fieldstype,align:'center'};
					if(d.isalign=='1')d1.align='left';
					if(d.isalign=='2')d1.align='right';
					if(d.ispx=='1')d1.sortable=true;
					if(d.iseditlx!='0')d1.editor=true;
					if(d1.type=='uploadimg')d1.renderer=function(v){
						if(v)return '<img src="'+v+'" onclick="js.viewimg(this)" height="30">';
					}
					d2 = this.setcinfo[d.fields];
					if(d2)d1 = js.apply(d1, d2);
					da.push(d1);
				}
			}
			this.table.setColumns(da);
		}
	
		this.setcolumns=function(fid, cnas){
			this.setcinfo[fid]=cnas;
			if(!this.table)return;
			var d = false,i,ad=this.table.columns,len=ad.length,oi=-1;
			for(i=0;i<len;i++){
				if(ad[i].dataIndex==fid){
					d = ad[i];
					oi= i;
					break;
				}
			}
			if(d){
				d = js.apply(d, cnas);
				this.table.columns[oi]=d;
			}
		}
		
		this.xiang=function(id){
			xcy.openxiang({
				num:this.num,
				mid:id,
				name:this.name,
				cnum:this.cnum,
				funitid:this.funitid,
				xiangwidth:this.xiangwidth,
				ismodal:true,
				tableid:this.table.getid()
			});
		}
		
		this.addlefthtml = function(txt){
			var str = '<td nowrap style="padding-right:10px">';
			str+=''+txt+'</td>';
			$('#toolleft_'+rand+'').after(str);
		}
		
		this.addopen=function(mod){
			var can = {
				num:this.num,
				mid:0,
				name:this.name,
				ismodal:mod,
				cnum:this.cnum,
				funitid:this.funitid,
				data:{},
				tableid:this.table.getid()
			};
			var odb = this.onaddbefore();
			if(odb && typeof(odb)=='object')for(var i in odb)can[i]=odb[i];
			if(odb && typeof(odb)=='string'){
				js.show.error(odb);
				return;
			}
			xcy.openinput(can,can.data);
		}
		
		this.addmenu=function(o1){
			importjs('menu', 'create', {
				data:[{name:'新增',ismodal:true},{name:'新增(新窗口)',ismodal:false}],
				obj:$(o1),
				onclick:function(d){
					me.addopen(d.ismodal);
				}
			});
		}
		
		//搜索
		this.search=function(s1){
			var d = this.onsearchbefore();
			if(d && typeof(d)=='string'){js.show.error(d);return;}
			var d1 = (typeof(d)=='object') ? d : {};
			d1.key = jm.base64encode(s1);
			this.setparams(d1);
		}
		
		this.setparams= function(cans){
			this.table.setparams(cans, true);
		}
		
		this.getacturl = function(act,bh){
			if(!bh)bh = this.num;
			var url = 'api/flow/action?num='+bh+'&act='+act+'';
			if(this.cnum)url+='&cnum='+this.cnum+'';
			return url;
		}
		

		this.init();
	}
	
	
}